<template>
    <div class="allotRoles public">
        <!-- 面包屑 -->
      	<el-breadcrumb separator=">" class="crumbs">
                <el-breadcrumb-item :to="{ path: '/duty'}">职位管理</el-breadcrumb-item>
      	  	<el-breadcrumb-item :to="{ path: '/allotRoles'}">分配角色</el-breadcrumb-item>
      	</el-breadcrumb>
          <el-row>
              <el-col v-for="item in roleList" :span="6" class="checkedBox"><el-checkbox v-model="item.checked">{{item.roleName}}</el-checkbox></el-col>
              
          </el-row>
          <el-button @click="submit()">提交</el-button>
    </div>
</template>


<script>
    export default {
        name:'allotRoles',
        data () {
            return{
                roleList:[],
                checked:[]
            }
        },
        methods:{
            submit () {
                // 把所有选中的推入数组
                for(let i = 0;i < this.roleList.length;i++){
                    if(this.roleList[i].checked){
                        this.checked.push(this.roleList[i].id)
                    }
                }
                var condition = {
                    id:this.$route.query.id,
                    roleList:this.checked
                }
                this.$http.post(this._path + '/positionManage/updateRoleToPosition',condition).then((res) =>{
                    if("0000" == res.data.resultCode){
                        this.$message({
                            message:'保存成功',
                            type:'success',
                            duration:1500,
                            showClose:true
                        })
                        this.$router.push('/duty')
                    }else{
                        this.requestError(res,this)
                    }
                })
            }
        },
        created () {
            var condition = {
                positionId :this.$route.query.id
            }
            this.$http.post(this._path + '/positionManage/selectAllRoles',condition).then((res) =>{
                if("0000" == res.data.resultCode){
                    this.roleList = res.data.data.roleList;
                    for(let i = 0; i < this.roleList.length; i++){
                        if('1' == this.roleList[i].checked){
                            this.roleList[i].checked = true;
                        }
                        if('0' == this.roleList[i].checked){
                            this.roleList[i].checked = false;
                        }
                    }
                }else{
                    this.requestError(res,this)
                }
                
            })
        }
    }
</script>
<style scoped lang="scss">
@import "./../style/allotRole.scss";
@import "./../style/public.scss"

</style>